package com.powerunion.datacollection.service;

import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.ActivityDao;
import com.powerunion.datacollection.dao.DaoException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.model.ActivityInstance;
import com.powerunion.datacollection.model.ApproveActivityInstance;
import com.powerunion.util.log.LogHelper;

public class ActivityManager {
	private static Logger logger = LogHelper.getInstance().getLogger(
			ActivityManager.class);
	private ActivityDao dao = null;

	public ActivityManager() {
		super();
		this.dao = (ActivityDao) DaoFactory.getInstance().getDao(
				ActivityDao.class);
	}

	public ApproveActivityInstance getParentActivity(String childId)
	 throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(childId)) {
			String[] args = { "childId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter childId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getParentActivity(childId);
	}
	
	public boolean updateActivityState(int state,String id)throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.updateActivityState(state, id);
	}
	
	public boolean updateActivityState4Child(int state,String id)throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.updateActivityState4Child(state, id);
	}
	
	public List findAllActivityByProcId(String procId) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(procId)) {
			String[] args = { "procId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter procId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.findAllActivityByProcId(procId);
	}
	
	public ActivityInstance getActivityInfoByKey(String id) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getActivityInfoByKey(id);
	}
	
	public List getChildren(String paraId) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(paraId)) {
			String[] args = { "paraId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter paraId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getChildren(paraId);
	}
	
	public String getNextActivity(String currentActivityId) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(currentActivityId)) {
			String[] args = { "currentActivityId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter currentActivityId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		StringBuffer result = new StringBuffer();
		List activities = this.dao.getNextActivity(currentActivityId);
		// 转化成JSON数组
		JSONArray jsonarr = JSONArray.fromObject(activities);
		result.append("{results:")
		.append(jsonarr).append("}");
		
		return result.toString();
	}
	
	public ApproveActivityInstance getApproveActivityInstance(String activityId)
	 throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(activityId)) {
			String[] args = { "activityId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter activityId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getApproveActivityInstance(activityId);
	}
	/**
	 * 更改某个节点的位置
	 * @param newParentId 新的父节点id
	 * @param activityId  节点的id
	 * @return
	 * @throws DaoException
	 */
	public boolean reconfigActivityPosition(String newParentId,String activityId) throws DaoException{
		// 参数为空
		if (StringUtils.isEmpty(newParentId)) {
			String[] args = { "newParentId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter newParentId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(activityId)) {
			String[] args = { "activityId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter activityId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.reconfigActivityPosition(newParentId, activityId);
	}
	
	public boolean dynamicAddApproveUsers(String actId,String approveUsers) throws DaoException{
		// 参数为空
		if (StringUtils.isEmpty(actId)) {
			String[] args = { "actId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter actId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(approveUsers)) {
			String[] args = { "approveUsers" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter approveUsers NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.dynamicAddApproveUsers(actId, approveUsers);
	}
}
